const App = {
    mixins: [Save],//配置文件，路径/utils/mixins.js
    data() {
        return {
            natural_resources_usage: [],
            type: [],
            isColor: false,
            isfirst: true,
        };
    },
    mounted() {
        this.getType();
        if (this.action == 'Modify') {
            this.getData1();
        }
    },
    watch: {
        'dataForm.Natural_Resource_Color'(val) {
            if (this.action == 'Modify' && this.isfirst) {
                this.isfirst = false;
                if (val) {
                    this.isColor = true;
                }
            }
            if (val == null) {
                this.dataForm.Natural_Resource_Color = '';
            }
        }
    },
    methods: {
        inputUsage(val) {
            let landcolor = '';
            switch (val) {
                case '乔木林地':
                    landcolor = '#6cff6c'
                    break
                case '交通服务场站用地':
                    landcolor = '#aaa9a9'
                    break
                case '公园与绿地':
                    landcolor = '#81c35d'
                    break
                case '公路用地':
                    landcolor = '#ffe2a9'
                    break
                case '其他园地':
                    landcolor = '#e7cce2'
                    break
                case '其他林地':
                    landcolor = '#00b800'
                    break
                case '天然牧草地':
                    landcolor = '#c8e3a0'
                    break
                case '其他草地':
                    landcolor = '#487b16'
                    break
                case '内陆滩涂':
                    landcolor = '#b3def8'
                    break
                case '农村宅基地':
                    landcolor = '#ffcc99'
                    break
                case '农村道路':
                    landcolor = '#8b4513'
                    break
                case '坑塘水面':
                    landcolor = '#add8e6'
                    break
                case '城镇住宅用地':
                    landcolor = '#ffa500'
                    break
                case '城镇村道路用地':
                    landcolor = '#ff61b3'
                    break
                case '旱地':
                    landcolor = '#ffff99'
                    break
                case '机关团体新闻出版用地':
                    landcolor = '#808080'
                    break
                case '果园':
                    landcolor = '#d6a7c9'
                    break
                case '森林沼泽':
                    landcolor = '#31ad69'
                    break
                case '水工建筑用地':
                    landcolor = '#48623c'
                    break
                case '水浇地':
                    landcolor = '#fcea9e'
                    break
                case '水田':
                    landcolor = '#f8d072'
                    break
                case '沟渠':
                    landcolor = '#a0b45a'
                    break
                case '河流水面':
                    landcolor = '#3c9682'
                    break
                case '灌丛沼泽':
                    landcolor = '#008080'
                    break
                case '沼泽草地':
                    landcolor = '#6c7a5a'
                    break
                case '灌木林地':
                    landcolor = '#b1b100'
                    break
                case '特殊用地':
                    landcolor = '#ef80f0'
                    break
                case '科教文卫用地':
                    landcolor = '#f1a5b4'
                    break
                case '空闲地':
                    landcolor = '#e1dce1'
                    break
                case '裸土地':
                    landcolor = '#d7c8b9'
                    break
                case '设施农用地':
                    landcolor = '#a0afb9'
                    break
                case '采矿用地':
                    landcolor = '#c59a8c'
                    break
                case '商业服务业设施用地':
                    landcolor = '#ff7878'
                    break
                default:
                    //其他所有的土地类型
                    landcolor = '#e4fcc5'
            }
            if (this.isColor) {
                let hex = landcolor.replace(/^#/, '');
                if (hex.length === 3) {
                    hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];
                }

                const num = parseInt(hex, 16);
                const r = (num >> 16) & 255; // 提取红色分量
                const g = (num >> 8) & 255;  // 提取绿色分量
                const b = num & 255;

                this.dataForm.Natural_Resource_Color = `rgb(${r}, ${g}, ${b})`;
            } else {
                this.dataForm.Natural_Resource_Color = ''
            }
        },
        handleChange() {
            this.inputUsage(this.dataForm.Natural_Resource_Usage_Name)
        },
        getType() {
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                method: "post",
                data: {
                    Data: 'natural_resources_type',
                    Condition: `Row_Status='0'`,
                },
                success: (res) => {
                    this.type = res;
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
            });
        },
        getData1() {
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
                method: "post",
                data: {
                    Data: 'natural_resources_usage',
                    Condition: `Parent_SerialNumber='${this.nodeKey}'`,
                },
                success: (res) => {
                    this.natural_resources_usage = res;
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
            });
        },
        async handleSubmit() {
            const allValid = await this.validateAllForms();
            if (allValid) {
                this.loading = true;
                let ls_URL = window.location.href;
                const action = getQueryObject(ls_URL, "action");   // 获取 action 参数
                let lJSONObject_Data_Options = new Object();
                lJSONObject_Data_Options = JSON.parse(document.getElementById("MainTable").getAttribute("data-options"));

                let ls_MainTableName = lJSONObject_Data_Options.TableName;
                let ls_DataForm = lJSONObject_Data_Options.form;
                let ls_primary_key = lJSONObject_Data_Options.primary_key;
                //没有子表下面内容就会报错，try/catch抛出异常
                try {
                    //可能涉及多个子表
                    document.getElementById("ChildTable").querySelectorAll(".el-descriptions").forEach(item => {
                        let lJSONObject_ChildData_Options = new Object();
                        lJSONObject_ChildData_Options = JSON.parse(item.getAttribute("data-options"));

                        let ls_ChildTableName = lJSONObject_ChildData_Options.TableName;
                        let ls_ChildDataForm = lJSONObject_ChildData_Options.form;
                        // let ls_ParentTableName = lJSONObject_ChildData_Options.ParentTableName;
                        if (this[ls_DataForm][ls_primary_key]) {
                            this[ls_ChildDataForm].forEach(item => {
                                item[ls_primary_key] = this[ls_DataForm][ls_primary_key];
                            })
                        }
                        //建立子表结构体
                        let JSONArray_ChildRecord = new Array();
                        JSONArray_ChildRecord = this[ls_ChildDataForm];
                        if (!this[ls_DataForm].Children) {
                            this[ls_DataForm].Children = new Object();
                        }
                        this[ls_DataForm].Children[ls_ChildTableName] = JSONArray_ChildRecord;
                    })
                } catch (error) {

                }
                //建立新增提交结构
                let lJSONObject_DataSet = new Object();
                if (action == 'Add') {
                    if (this.nodeKey) {
                        this[ls_DataForm].Parent_SerialNumber = this.nodeKey;
                    }
                    let lJSONArray_Record = new Array();
                    lJSONArray_Record.push(this[ls_DataForm]);
                    let lJSONObject_Table = new Object();
                    lJSONObject_Table[ls_MainTableName] = lJSONArray_Record;
                    lJSONObject_DataSet[action] = lJSONObject_Table;
                } else if (action == 'Modify') {
                    const { add, modify } = this.deal(this[ls_DataForm]);
                    let lJSONArray_Record = new Array();
                    lJSONArray_Record.push(modify);

                    let lJSONObject_Table = new Object();
                    lJSONObject_Table[ls_MainTableName] = lJSONArray_Record;

                    this.natural_resources_usage.forEach(item => {
                        item.Parent_SerialNumber = this.dataForm.Natural_Resource_Usage_SerialNumber
                        if (item.Parent_SerialNumber != item.Parent_SerialNumber_Original) {
                            lJSONArray_Record.push(item);
                        }
                    })

                    lJSONObject_DataSet[action] = lJSONObject_Table;
                    lJSONObject_DataSet['Add'] = new Object();
                    if (add != null) {
                        let lJSONArray_Record_Add = new Array();
                        lJSONArray_Record_Add.push(add);

                        let lJSONObject_Table_Add = new Object();
                        lJSONObject_Table_Add[ls_MainTableName] = lJSONArray_Record_Add;

                        lJSONObject_DataSet['Add'] = lJSONObject_Table_Add;
                    }
                }
                let Operation_Main = JSON.stringify(deepClone(lJSONObject_DataSet));
                if (!lJSONObject_DataSet['Add']) {
                    lJSONObject_DataSet['Add'] = new Object();
                }
                lJSONObject_DataSet['Add']['base_operation_log'] = [{
                    Operation_Type: '0',
                    Operation_Path: this.relativePath,
                    Operation_Main,
                    Operation_Interface: "generalSave",
                    Operation_IP: getIpAddress()
                }]
                submitSave(lJSONObject_DataSet, (res) => {
                    if (res.Status == 'success') {
                        window.location.href = './index.html'
                    }
                    this.loading = false;
                })
                // $.ajax({
                //     headers: requestHeaders(),
                //     url: `${mh}/generalBusiness/generalSave`,
                //     method: "post",
                //     data: {
                //         Data: JSON.stringify(lJSONObject_DataSet),
                //     },
                //     success: (res) => {
                //         if (res.Status == 'success') {
                //             window.location.href = './index.html'
                //             // window.history.go(-1);
                //         }
                //     },
                //     complete: (XMLHttpRequest) => {
                //         this.loading = false;
                //         requestPretreatment(XMLHttpRequest);
                //     }
                // });
            }
        },
    },
};
const { createApp, ref } = Vue;
const app = createApp(App);
app.use(ElementPlus, { locale: zhCn });
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component);
}
app.component('yl-upload', ylUpload);
app.component('yl-text-editor', ylTextEditor);
const ElMessage = ElementPlus.ElMessage;
app.mount("#app");
